package com.wtwd.wtwddevice.listener.deviceServerMsg.handler.impl;

import com.wtwd.device.annotation.MsgType;
import com.wtwd.device.constant.*;
import com.wtwd.device.model.vo.AppAlarmPowerRecordVo;
import com.wtwd.device.mqtentity.MessageBase;
import com.wtwd.device.protocol.AppProtocol;
import com.wtwd.device.protocol.WTProtocol;
import com.wtwd.device.util.DateUtils;
import com.wtwd.wtwddevice.listener.deviceServerMsg.handler.IMsgHandler;
import com.wtwd.wtwddevice.model.entity.AlarmPower;
import com.wtwd.wtwddevice.model.entity.Device;
import com.wtwd.wtwddevice.model.entity.DeviceStatus;
import com.wtwd.wtwddevice.mq.PushMsgService;
import com.wtwd.wtwddevice.service.IAlarmPowerService;
import com.wtwd.wtwddevice.service.IAppCommService;
import com.wtwd.wtwddevice.service.IDeviceService;
import com.wtwd.wtwddevice.service.IDeviceStatusService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;

@MsgType(MsgTypeEnum.ALARM_POWER)
@Component
public class AlarmPowerHandlerImpl implements IMsgHandler {
    @Resource
    private IDeviceStatusService deviceStatusService;

    @Resource
    private IDeviceService deviceService;

    @Resource
    private IAppCommService appCommService;

    @Autowired
    private IAlarmPowerService alarmPowerService;


    /**
     *
     * @param messageBase
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void handleMsg(MessageBase messageBase) {
        WTProtocol wtProtocol = messageBase.getWtProtocol();
        String[] split = wtProtocol.getBody().split(SeparatorConstant.POUND_SIGN);

        Device device = deviceService.getDeviceInfoByImei(messageBase.getDeviceKey());
        if (device != null) {
            DeviceStatus deviceStatus = new DeviceStatus();
            AlarmPower alarmPower = new AlarmPower();

            alarmPower.setDeviceId(device.getId());
            alarmPower.setType(Byte.valueOf(split[0]));
            alarmPower.setSurplusPower(split[1]);
            alarmPower.setNumber(wtProtocol.getNumber());
            alarmPower.setTime(wtProtocol.getTime());
            alarmPower.setCreateTime(System.currentTimeMillis());
            alarmPower.setUpdateTime(System.currentTimeMillis());

            deviceStatus.setBattery(split[1]);
            deviceStatus.setDeviceId(device.getId());
            deviceStatus.setUpdateTime(System.currentTimeMillis());

            //离线
            if(split[0].equals(AlarmPowerConstant.AUTOMATIC_SHUTDOWN_ALARM) || split[0].equals(AlarmPowerConstant.SHUTDOWN_ALARM)){
                deviceStatus.setStatus(DeviceLoginEnum.DEVICE_OFFLINE);
                deviceStatus.setDownlineTime(System.currentTimeMillis());
            }
            int editDeviceStatus = deviceStatusService.editDeviceStatus(deviceStatus);
            int addAlarmPower = alarmPowerService.addAlarmPower(alarmPower);

            //关机推送离线状态
            if(editDeviceStatus > 0 && addAlarmPower > 0 &&  (split[0].equals(AlarmPowerConstant.SHUTDOWN_ALARM) || split[0].equals(AlarmPowerConstant.AUTOMATIC_SHUTDOWN_ALARM))){
                AppProtocol appProtocol = AppProtocol.builder()
                        .imei(wtProtocol.getImei())
                        .cmdName(MsgTypeEnum.DEVICE_STATE_PUSH.getCmdName())
                        .data(DeviceLoginEnum.DEVICE_OFFLINE)
                        .build();
                appCommService.pushDeviceState(device.getId(),appProtocol);
            }
        }
        return;
    }

}
